package com.c.ctgproject.ui.files

import android.content.Intent
import androidx.appcompat.app.AlertDialog
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import com.c.ctgproject.R
import com.c.ctgproject.adapter.file.FileFirstExamineAdapter
import com.c.ctgproject.base.BaseActivity
import com.c.ctgproject.entity.file.FirstExamineCarEntity
import com.c.ctgproject.entity.file.FirstExamineCarInfoEntity
import com.c.ctgproject.ui.files.callback.IsDeleteInterface
import com.c.ctgproject.ui.files.callback.IsDeleteManager
import com.c.ctgproject.ui.newvehicles.Newvehicles
import com.c.ctgproject.ui.vehicleinspection.VehicleInspectionActivity
import com.c.ctgproject.utils.*
import kotlinx.android.synthetic.main.activity_file_first_examine.mCarRV
import kotlinx.android.synthetic.main.activity_file_first_examine.mRefresh
import kotlinx.android.synthetic.main.activity_file_first_examine.mSearchET
import kotlinx.android.synthetic.main.activity_file_first_examine.mSearchTV
import kotlinx.android.synthetic.main.activity_inputplate.*

/**
 *作者:LiBW
 *创建日期: 2020/10/21
 *描述:待初验车辆列表 页面
 */
class FileFirstExamineActivity : BaseActivity(), FileFirstExamineAdapter.CarCallback, IsDeleteInterface {
    private val mViewModel: FileFirstExamineViewModel by lazy { ViewModelProvider(this)[FileFirstExamineViewModel::class.java] }
    private var page = 1
    private var size = 20
    private var searchText = ""
    private val mList = ArrayList<FirstExamineCarEntity.DataBean.ListBean>()
    private val mAdapter: FileFirstExamineAdapter by lazy { FileFirstExamineAdapter(this, mList, this) }

    override fun event() {
        getData()
        IsDeleteManager.getListener(this)
        changeTransparentStatusBar()
        mCarRV.adapter = mAdapter
        mCarRV.layoutManager = LinearLayoutManager(this)
        mViewModel.result.observe(this, Observer {
            loading.dismiss()
            mRefresh.over()
            MyLog.d("待初验车辆列表:$it")
            val data = initData(it)
            if (data.status == 0) {
                val entity = getGson().fromJson(it, FirstExamineCarEntity::class.java).data
                entity?.list?.let { list ->
                    mList.addAll(list)
                    mAdapter.notifyDataSetChanged()
                }
            } else {
                showToast(data.msg)
            }
        })
        mViewModel.carResult.observe(this, {
            loading.dismiss()
            mRefresh.over()
            MyLog.d("车辆回调:$it")
            val data = initData(it)
            if (data.status == 0) {
                //进入车辆初检
                val entity = getGson().fromJson(it, FirstExamineCarInfoEntity::class.java).data
                entity?.let {
                    val intent = Intent(applicationContext, VehicleInspectionActivity::class.java)
                    intent.putExtra("carOwner", entity.carOwner)
                    intent.putExtra("ownerPhone", entity.ownerPhone)
                    intent.putExtra("ownerPlate", entity.ownerPlate)
                    intent.putExtra("carId", entity.carId)
                    intent.putExtra("salesman", entity.salesman)
                    startActivity(intent)
                }
                if (entity == null) {
                    showToast("出现问题,请稍后再试")
                }
            } else {
                //新建车辆
                AlertDialog.Builder(this).setTitle("提示！")
                    .setMessage(data.msg.handlerNull()).setPositiveButton("新建车辆") { _, _ ->
                        val intent = Intent(applicationContext, Newvehicles::class.java)
                        intent.putExtra("carPlate", etPlateNumber.text.handlerNull())
                        startActivity(intent)
                    }.setNegativeButton("取消") { dialogInterface, _ ->
                        dialogInterface.dismiss()
                    }.create().show()
            }
        })


        mViewModel.error.observe(this, Observer {
            mRefresh.over()
            loading.dismiss()
            MyLog.d("错误:$it")
            showToast(it)
        })
    }



    private fun refreshData(){
        page = 1
        searchText = ""
        mList.clear()
        getData()
    }

    private fun getData() {
        loading.show()
        mViewModel.getCarList(page, size, searchText)
    }

    override fun click() {
        mSearchET.handlerText {
            if (it.isEmpty()) {
                searchText = ""
                mList.clear()
                page = 1
                getData()
            }
        }

        //搜索
        mSearchTV.setOnClickListener {
            searchText = mSearchET.text.toString()
            page = 1
            mList.clear()
            getData()
        }

        mRefresh.setOnLoadMoreListener {
            page++
            getData()
        }
        mRefresh.setOnRefreshListener {
            page = 1
            mList.clear()
            getData()
        }
    }

    override fun getLayout() = R.layout.activity_file_first_examine

    override fun onItemClick(ownerPlate: String) {
        mViewModel.getFirstExamineCarByCarPlate(ownerPlate)
    }

    override fun isDelete(ownerPlate: String) {
        MyLog.d("删除掉了,车牌:$ownerPlate")
        mList.forEach {
            if (it.ownerPlate == ownerPlate) {
                mList.remove(it)
                mAdapter.notifyDataSetChanged()
            }
        }
    }
}